// 雇佣兵 插件

string author = "Live";
string pluginName = "Mercenary 雇佣兵插件";
string version = "1.0";

// 预缓存军人模型列表
const array<string> s_PlayerModels = {
"models/survivors/common/military01.mdl",
"models/survivors/common/military02.mdl"
};

// 插件初始化
void OnPluginInit()
{
	// 设置插件信息
	PluginData::SetAuthor(author);
	PluginData::SetName(pluginName);
	PluginData::SetVersion(version);

	// 提示
	ThePresident.InfoFeed("插件:"+pluginName+"| 版本:"+version+"| 作者:"+author,false);

	Events::Player::PlayerSay.Hook( @PlayerSay );

	ThePresident_OnMapStart();
    
}

// 地图开始
void ThePresident_OnMapStart()
{
    ThePresident_OnRoundStart();
}

// 回合开始
void ThePresident_OnRoundStart()
{
	PrecacheFileModel();
}

// 缓存模型
void PrecacheFileModel(){
	// 缓存所有角色模型
    for( uint i = 0; i < s_PlayerModels.length(); i++ ){
        Engine.PrecacheFile( model, s_PlayerModels[i] );
    }
}

// 当玩家say
HookReturnCode PlayerSay(CTerrorPlayer@ pPlayer, CASCommand@ pArgs)
{
	string arg = pArgs.Arg( 1 );
	string arg1 = "";
	string arg2 = "";

	// 切分字符串(这样就可以用空格传一个命令参数了)
	int subIndex = arg.findFirst(" ",0);
	if(not (subIndex < 0)){
		arg1 = arg.substr(0,subIndex);
		arg2 = arg.substr(subIndex+1,-1);
		//Chat.PrintToChat(all,""+arg1+"|"+arg2);
	}else{
		arg1 = arg;
	}

	if(Utils.StrEql("/mer",arg1)){
		if(arg2!=""){
			for(int i=0;i<Utils.StringToInt(arg2);i++){
				CBaseEntity@ suivivor = EntityCreator::Create("survivor_riot",getPlayerAimVector(pPlayer),pPlayer.GetAbsAngles());
				suivivor.SetModel( s_PlayerModels[ Math::RandomInt( 0, s_PlayerModels.length() - 1 ) ] );
			}
		}else{
			CBaseEntity@ suivivor = EntityCreator::Create("survivor_riot",getPlayerAimVector(pPlayer),pPlayer.GetAbsAngles());
			suivivor.SetModel( s_PlayerModels[ Math::RandomInt( 0, s_PlayerModels.length() - 1 ) ] );
		}
			
	}

	return HOOK_CONTINUE;
}


// 获取玩家准心坐标
Vector getPlayerAimVector(CTerrorPlayer@ pPlayer){
	// 获取玩家眼睛坐标
	Vector vecEyes = pPlayer.EyePosition();

	// 获取角度向量
	Vector vec;
	Globals.AngleVectors( pPlayer.EyeAngles(), vec );

	// 获得一个从眼睛坐标依据向量500米后的坐标
	Vector vec_end = vecEyes + vec * 500;
	
	// 线路追踪器
	CGameTrace tr;
	Utils.TraceLine( vecEyes, vec_end, MASK_SHOT, pPlayer, COLLISION_GROUP_NONE, tr );

	// 准心坐标
	return tr.endpos;
}

        